/**
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

#ifndef EXAMPLES_ACTIVATION_ADJUST_SOFTMAX_RES_CUSTOM_H
#define EXAMPLES_ACTIVATION_ADJUST_SOFTMAX_RES_CUSTOM_H
#include "../op_host/adjust_softmax_res_custom_tiling.h"
#include "kernel_operator.h"

namespace MyCustomKernel {
constexpr int32_t BUFFER_NUM = 1;
constexpr int32_t HEIGHT = 32;
constexpr int32_t WIDTH = 32;
constexpr uint32_t FROM = 0xFF7FFFFF;
constexpr float TO = 0.0;

template <typename T>
class KernelSoftmax {
public:
    __aicore__ inline KernelSoftmax()
    {}
    __aicore__ inline void Init(GM_ADDR srcGm, GM_ADDR dstGm, GM_ADDR maxGM, AdjustSoftMaxResTilingData tiling, AscendC::TPipe* pipeIn)
    {
        pipe = pipeIn;
        elementNumPerBlk = HEIGHT / sizeof(T);
        height = tiling.height;
        width = tiling.width;
        srcGlobal.SetGlobalBuffer((__gm__ T *)srcGm);
        maxGlobal.SetGlobalBuffer((__gm__ T *)maxGM);
        dstGlobal.SetGlobalBuffer((__gm__ T *)dstGm);
        pipe->InitBuffer(inQueueSrc, BUFFER_NUM, height * width * sizeof(T));
        pipe->InitBuffer(inQueueMax, BUFFER_NUM, height * elementNumPerBlk * sizeof(T));
        pipe->InitBuffer(outQueueDst, BUFFER_NUM, height * width * sizeof(T));
        pipe->InitBuffer(calcBuf, height * width * sizeof(T));
    }
    __aicore__ inline void Process()
    {
        CopyIn();
        Compute();
        CopyOut();
    }

private:
    __aicore__ inline void CopyIn()
    {
        AscendC::LocalTensor<T> srcLocal = inQueueSrc.AllocTensor<T>();
        AscendC::LocalTensor<T> maxLocal = inQueueMax.AllocTensor<T>();
        AscendC::DataCopy(srcLocal, srcGlobal, height * width);
        AscendC::DataCopy(maxLocal, maxGlobal, height * elementNumPerBlk);
        inQueueSrc.EnQue(srcLocal);
        inQueueMax.EnQue(maxLocal);
    }
    __aicore__ inline void Compute()
    {
        AscendC::LocalTensor<T> srcLocal = inQueueSrc.DeQue<T>();
        AscendC::LocalTensor<T> maxLocal = inQueueMax.DeQue<T>();
        AscendC::LocalTensor<T> dstLocal = outQueueDst.AllocTensor<T>();
        AscendC::LocalTensor<T> tmpTensor = calcBuf.Get<T>();
        AscendC::SoftMaxShapeInfo srcShape = {height, width, height, width};
        AscendC::AdjustSoftMaxRes<T, T>(srcLocal, maxLocal, FROM, TO, srcShape);
        AscendC::DataCopy(tmpTensor, srcLocal, height * width);
        AscendC::DataCopy(dstLocal, tmpTensor, height * width);

        outQueueDst.EnQue<T>(dstLocal);
        inQueueMax.FreeTensor(maxLocal);
        inQueueSrc.FreeTensor(srcLocal);
        calcBuf.FreeTensor(tmpTensor);
    }
    __aicore__ inline void CopyOut()
    {
        AscendC::LocalTensor<T> dstLocal = outQueueDst.DeQue<T>();
        AscendC::DataCopy(dstGlobal, dstLocal, height * width);
        outQueueDst.FreeTensor(dstLocal);
    }

private:
    AscendC::TPipe* pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueSrc;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueMax;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueDst;
    AscendC::TBuf<AscendC::TPosition::VECCALC> calcBuf;
    AscendC::GlobalTensor<T> srcGlobal;
    AscendC::GlobalTensor<T> maxGlobal; 
    AscendC::GlobalTensor<T> dstGlobal;
    uint32_t elementNumPerBlk = 0;
    uint32_t height = 0;
    uint32_t width = 0;
};
}
#endif // EXAMPLES_ACTIVATION_ADJUST_SOFTMAX_RES_CUSTOM_H